#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QAction>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QDialog>
#include <QMessageBox>
#include "setvalue.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWinProc();
    setRightClickMenu();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(),QColor(Qt::GlobalColor::transparent));
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton){
        m_StartPoint = frameGeometry().topLeft() - e->globalPos();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    this->move(e->globalPos() + m_StartPoint);
}

void MainWindow::setWinProc()
{
    setWindowFlag(Qt::FramelessWindowHint);/*enable the framelss in windows platform*/
    setWindowFlag(Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground);/*Translucent Background.*/
    ui->menubar->setVisible(false);
    ui->statusbar->setVisible(false);
    ui->progressBar->setAlignment(Qt::AlignHCenter);
    ui->progressBar->setTextVisible(true);
    ui->progressBar->setFormat("%v");
    ui->progressBar->setMaximum(100);
    ui->progressBar->setMinimum(0);
    ui->progressBar->setValue(0);
}

void MainWindow::setRightClickMenu()
{
    /*Define the actions*/
    m_winRghtClick_Restart = new QAction("Restart",this);
    m_winRghtClick_SetValue = new QAction("Set Value",this);
    m_winRghtClick_Start = new QAction("start",this);
    m_winRghtClick_Exit = new QAction("Exit",this);

    /*Initialize the menu*/
    m_winMenu = new QMenu(this);

    m_winMenu->addAction(m_winRghtClick_Restart);
    m_winMenu->addAction(m_winRghtClick_SetValue);
    m_winMenu->addAction(m_winRghtClick_Start);
    m_winMenu->addAction(m_winRghtClick_Exit);

    connect(m_winRghtClick_Restart,&QAction::triggered,[=](){
        m_current = 0;
        if(timer != nullptr){
            timer->start(1000);
            qDebug() <<"Timer is restarted";
        }
    });

    connect(m_winRghtClick_SetValue,&QAction::triggered,[=](){
        SetValueFrm = new SetValue();
        SetValueFrm->setWindowFlags(Qt::FramelessWindowHint);
        this->hide();
        SetValueFrm->show();
        connect(SetValueFrm,&SetValue::SetMaxValue,this,[=](quint32 maxValue){
            m_Prog_Max = maxValue;
            m_current = 0;
            qDebug() <<"Value setting + " + QString::number(m_Prog_Max);
            ui->progressBar->setMaximum(m_Prog_Max*60);
            ui->progressBar->setMinimum(0);
            ui->progressBar->setValue(m_current);
            SetValueFrm->deleteLater();
            this->show();

//            if(timer != Q_NULLPTR){
//                timer->stop();
//            }
        });
    });

    connect(m_winRghtClick_Start,&QAction::triggered,[=](){
        timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, [=](){
            m_current++;
            ui->progressBar->setValue(m_current);
            if(m_current >= m_Prog_Max*60){
                QMessageBox msgBox(QMessageBox::NoIcon,"Inspire","Time is end!",QMessageBox::Yes);
                (void)msgBox.exec();
                timer->stop();
            }
            qDebug() <<"second + " + QString::number(m_current);
        });
        timer->start(1000);
    });

    connect(m_winRghtClick_Exit,&QAction::triggered,[=](){
        this->close();
    });

    ui->progressBar->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(ui->progressBar,&QProgressBar::customContextMenuRequested,[=](const QPoint &pos){
        qDebug() << pos;
        m_winMenu->exec(QCursor::pos());
    });

}


void MainWindow::on_MainWindow_customContextMenuRequested(const QPoint &pos)
{

}
